//
//  CMDBPresentationController.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/14.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBPresentationController: UIPresentationController {
    
    //MARK:- 对外提供属性
    var presentedFrame : CGRect = CGRectZero
    
    private lazy var coverView : UIView = UIView()
    
    //MARK:- 系统回调
    override func containerViewWillLayoutSubviews() {
        
        //1.设置view的弹出尺寸
        presentedView()?.frame = presentedFrame
        
        //2.设置蒙版
        setupCoverView()
    }
}

extension CMDBPresentationController
{
    private func setupCoverView(){
        
        //1.添加蒙版
        containerView?.insertSubview(coverView, atIndex: 0)
        
        coverView.alpha = 0
        
        //2.设置蒙版属性
        coverView.backgroundColor = word38Color
        
        coverView.frame           = containerView!.bounds
        
        UIView.animateWithDuration(0.375) { 
            self.coverView.alpha = 1
        }
     
        //3.添加手势
        let tapGes                = UITapGestureRecognizer(target: self, action: #selector(coverViewClick))
        
        coverView.addGestureRecognizer(tapGes)
    }
    
    
}

extension CMDBPresentationController{
    
    @objc func coverViewClick(){
        
        
        UIView.animateWithDuration(0.375, animations: {
            UIView.animateWithDuration(0.375) {
                self.coverView.alpha = 0
            }
        })
        
        presentedViewController.dismissViewControllerAnimated(true) {
            
        }

        
    }
    
}

